﻿<template>
    <el-container>
        <el-main>
            <el-tree ref="tree"
                     :data="data"
                     node-key="id"
                     :defaultExpandAll="true"
                     show-checkbox
                     check-strictly
                     :default-checked-keys="defaultCheckedKeys">
            </el-tree>
        </el-main>
        <el-footer style="text-align:right;">
            <el-button @click="$emit('cancel')">取消</el-button>
            <el-button type="primary" @click="onSave">保存</el-button>
        </el-footer>
    </el-container>
</template>

<script>
    export default {
        props: ['providerKey'],
        data() {
            return {
                loading: false,
                dialogPermissionsVisible: false,
                data: [],
                defaultExpandedKeys: [],
                defaultCheckedKeys: [],
                dicPermissions: {}
            }
        },
        mounted() {
            var self = this;
            self.refresh(self.providerKey);
        },
        methods: {
            refresh(key) {
                var self = this;
                self.loading = true;
                self.data = [];
                self.defaultExpandedKeys = [];
                self.defaultCheckedKeys = [];
                self.dicPermissions = {};

                self.$http
                    .get('/api/abp/permissions', {
                        providerName: 'R',
                        providerKey: key
                    })
                    .then((res) => {

                        var _defaultCheckedKeys = [];
                        var root = {
                            id: '__root',
                            label: '全部',
                            children: [],
                            disabled: true
                        };
                        self.data.push(root);
                        self.defaultCheckedKeys.push(root.id);


                        for (var i1 in res.groups) {
                            var item1 = res.groups[i1];
                            var node1 = {
                                id: item1.name,
                                label: item1.displayName,
                                children: [],
                                disabled: true
                            };
                            root.children.push(node1);
                            self.defaultCheckedKeys.push(node1.id);

                            var list2 = item1.permissions.filter(o => { return o.parentName == null; });
                            for (var i2 in list2) {
                                var item2 = list2[i2];
                                var node2 = {
                                    id: item2.name,
                                    label: item2.displayName,
                                    children: []
                                };
                                node1.children.push(node2);
                                self.defaultCheckedKeys.push(node2.id);
                                if (item2.isGranted) {
                                    _defaultCheckedKeys.push(node2.id);
                                }
                                self.dicPermissions[item2.name] = item2.isGranted;

                                var list3 = item1.permissions.filter(o => { return o.parentName == item2.name; });
                                for (var i3 in list3) {
                                    var item3 = list3[i3];
                                    var node3 = {
                                        id: item3.name,
                                        label: item3.displayName,
                                        children: []
                                    };
                                    node2.children.push(node3);
                                    self.defaultCheckedKeys.push(node3.id);

                                    if (item3.isGranted) {
                                        _defaultCheckedKeys.push(node3.id);
                                    }
                                    self.dicPermissions[item3.name] = item3.isGranted;
                                }
                            }
                        }

                        setTimeout(() => {
                            self.defaultCheckedKeys = _defaultCheckedKeys;
                            self.loading = false;
                        }, 100);
                    });
            },
            onSave() {
                var self = this;
                for (var k in self.dicPermissions) {
                    self.dicPermissions[k] = false;
                }
                var _keys = self.$refs.tree.getCheckedKeys();
                for (var i in _keys) {
                    var _key = _keys[i];
                    self.dicPermissions[_key] = true;
                }

                var _permissions = [];
                for (var k in self.dicPermissions) {
                    _permissions.push({
                        name: k,
                        isGranted: self.dicPermissions[k]
                    });
                }
                self.$http
                    .put('/api/abp/permissions?providerName=R&providerKey=' + self.providerKey, {
                        permissions: _permissions
                    })
                    .then((res) => {
                        self.$emit("change", res);
                    });
            }
        }
    }
</script>

<style scoped>
</style>